package cn.gemiman.daoyi.module.cms.service.articledetailreadlogs;

import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.module.cms.controller.admin.articledetailreadlogs.vo.ArticleDetailReadLogsPageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.articledetailreadlogs.vo.ArticleDetailReadLogsSaveReqVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.articledetailreadlogs.ArticleDetailReadLogsDO;
import cn.gemiman.daoyi.module.cms.dal.mysql.articledetailreadlogs.ArticleDetailReadLogsMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.gemiman.daoyi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gemiman.daoyi.module.cms.enums.ErrorCodeConstants.ARTICLE_DETAIL_READ_LOGS_NOT_EXISTS;

/**
 * 阅读记录 Service 实现类
 *
 * @author 道一源码
 */
@Service
@Validated
public class ArticleDetailReadLogsServiceImpl implements ArticleDetailReadLogsService {

    @Resource
    private ArticleDetailReadLogsMapper articleDetailReadLogsMapper;

    @Override
    public Long createArticleDetailReadLogs(ArticleDetailReadLogsSaveReqVO createReqVO) {
        // 插入
        ArticleDetailReadLogsDO articleDetailReadLogs = BeanUtils.toBean(createReqVO, ArticleDetailReadLogsDO.class);
        articleDetailReadLogsMapper.insert(articleDetailReadLogs);
        // 返回
        return articleDetailReadLogs.getId();
    }

    @Override
    public void updateArticleDetailReadLogs(ArticleDetailReadLogsSaveReqVO updateReqVO) {
        // 校验存在
        validateArticleDetailReadLogsExists(updateReqVO.getId());
        // 更新
        ArticleDetailReadLogsDO updateObj = BeanUtils.toBean(updateReqVO, ArticleDetailReadLogsDO.class);
        articleDetailReadLogsMapper.updateById(updateObj);
    }

    @Override
    public void deleteArticleDetailReadLogs(Long id) {
        // 校验存在
        validateArticleDetailReadLogsExists(id);
        // 删除
        articleDetailReadLogsMapper.deleteById(id);
    }

    private void validateArticleDetailReadLogsExists(Long id) {
        if (articleDetailReadLogsMapper.selectById(id) == null) {
            throw exception(ARTICLE_DETAIL_READ_LOGS_NOT_EXISTS);
        }
    }

    @Override
    public ArticleDetailReadLogsDO getArticleDetailReadLogs(Long id) {
        return articleDetailReadLogsMapper.selectById(id);
    }

    @Override
    public PageResult<ArticleDetailReadLogsDO> getArticleDetailReadLogsPage(ArticleDetailReadLogsPageReqVO pageReqVO) {
        return articleDetailReadLogsMapper.selectPage(pageReqVO);
    }

}
